package com.XinYun.Library.modules.bookcart.delete.dao;

import com.XinYun.Library.modules.bookcart.delete.entity.Book;

import java.util.ArrayList;
import java.util.Scanner;

public class Shoppingcart {
    private ArrayList<Book> books;

    public Shoppingcart() {
        books = new ArrayList<>();
    }

    public void addBook(Book book) {
        books.add(book);
        System.out.println(book.getTitle() + " has been added to your cart.");
    }

    public void removeBook(String title) {
        boolean removed = false;
        for (int i = 0; i < books.size(); i++) {
            if (books.get(i).getTitle().equals(title)) {
                books.remove(i);
                System.out.println(title + " has been removed from your cart.");
                removed = true;
                break;
            }
        }
        if (!removed) {
            System.out.println(title + " is not in your cart.");
        }
    }

    public void displayCart() {
        if (books.size() == 0) {
            System.out.println("Your cart is empty.");
        } else {
            System.out.println("Books in your cart:");
            for (Book book : books) {
                System.out.println(" - " + book.getTitle() + " by " + book.getAuthor());
            }
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Shoppingcart cart = new Shoppingcart();
        Book book1 = new Book("The Great Gatsby", "F. Scott Fitzgerald");
        Book book2 = new Book("To Kill a Mockingbird", "Harper Lee");
        Book book3 = new Book("1984", "George Orwell");
        cart.addBook(book1);
        cart.addBook(book2);
        cart.addBook(book3);
        cart.displayCart();
        System.out.print("Enter the title of the book you want to remove: ");
        String title = scanner.nextLine();
        cart.removeBook(title);
        cart.displayCart();
    }
}
